<?php 
namespace app\system\admin;
use app\system\admin\Admin;
use think\Db;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

/**
 *  @author Lin
 *  @time 2021/7/22
 */
class Files extends Admin
{
    protected function initialize()
    {
        parent::initialize();
        set_time_limit(0);
    }

    function index(){
        $bgTime = 0;
        $endTime = 0;
        if ($this->request->isAjax()) {
            $where  = $data = [];
            $page   = $this->request->param('page/d', 1);
            $limit  = $this->request->param('limit/d', 15);
            $keyword    = $this->request->param('keyword/s');
            $groupId    = $this->request->param('group_id/d');
            $bgTime = $this->request->param('bgTime/s');
            $endTime = $this->request->param('endTime/s');

            
            
            if ($keyword) {
                $where[] = ['name','like','%'.$keyword.'%'];
            }

            $list = Db::name('files')->where($where)->page($page)->order('id desc')->limit($limit)->select();

            if($bgTime){
                $this->assign('bg_time',$bgTime);
                // $where[] = ['ctime','egt',strtotime($bgTime)];
                $today_start_time = strtotime($bgTime);
            }
            else{
                $today_start_time=strtotime(date("Y-m-d",time()));
                $this->assign('bg_time',0);
            }
            if($endTime){
                $today_end_time = strtotime($endTime);
                $this->assign('end_time',$endTime);
                // $where[] = ['ctime','elt',strtotime($endTime)];
            }
            else{
                $today_end_time = $today_start_time+86399;
                $this->assign('end_time',0);
            }
            // $today_start_time=strtotime(date("Y-m-d",time()));
            // $today_end_time = $today_start_time+86399;
            $yes_start_time = $today_start_time-86400;
            $yes_end_time = $today_start_time-1;
            foreach($list as $key=>&$value){
                if($value['pic']){
                    $value['pic'] = MYDOMAIN.$value['pic'];
                }
            }
            $data['data'] = $list;
            $data['count']  = Db::name('files')->count('id');
            $data['code']   = 0;

            return json($data);
        }
        $this->assign('bg_time',$bgTime);
        $this->assign('end_time',$endTime);
        return $this->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            $list = $this->request->post();
            
            $scene_check = Db::name('qrcode')->count('id');
            $scene_check += 1;
            $scene = "scenes".$scene_check;
            $data['qrcode'] = $this->getQrcode($scene);
            $data['ctime'] = time();
            $data['scene'] = $scene;
            $rs = Db::name('qrcode')->insert($data);
            if($rs){
                return $this->success('提交成功');
            }
            else{
                return $this->error('提交失败，请重试');
            }
        }
        else{
            $template = $this->request->param('template', 'form');
            return $this->fetch($template);
        }
    }

    function upload(){
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH.'public/upload');
        // var_dump($info);exit;
        if($info) {
            $data['code'] = 0;
            $data['msg'] = $info->getSaveName();
            $data['type'] = $info->getExtension();//获取图片的类型
            $file_info = $info->getInfo();
            if($data['type'] == 'jpg' || $data['type'] == 'gif' || $data['type'] == 'png' || $data['type'] == 'jpeg'){
                Db::name('files')->insert([
                    'pic' => 'upload/'.$data['msg'],
                    'name'=>$file_info['name'],
                    'size'=>sprintf('%.2f',$file_info['size']/1024/1024)."M",
                    'type'=>$data['type'],
                    'ctime' => time()
                ]);
                return $this->success('提交成功');
            }
        }else{
            return $this->error('提交失败，请重试');
        }
    }

    function del(){
        $id = input("get.id/d");
        $rs = Db::name('files')->where('id',$id)->delete();
        if($rs){
            return $this->success('删除成功');
        }
        else{
            return $this->error('删除失败，请重试');
        }
    }
}

?>